package com.zhangyu.shadow.ec.main.sort.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tepth.shadow.ui.recycler.BaseDecoration;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.core.net.RestClient;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.zhangyu.shadow.ec.R;
import com.zhangyu.shadow.ec.R2;
import com.zhangyu.shadow.ec.main.sort.SortDelegate;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @author zhangyu
 * @date 2017/11/7
 */

public class VerticalListDelegate extends ShadowDelegate {
    @BindView(R2.id.rv_vertical_menu_list)
    RecyclerView mRecyclerView;

    @Override
    public Object setLayout() {
        return R.layout.delegate_vertical_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initRecyclerView();
    }

    private void initRecyclerView() {
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setItemAnimator(null);
        mRecyclerView.addItemDecoration(BaseDecoration.create(ContextCompat.getColor(getContext(),
                R.color.app_background), 1));

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.builder().url("sort_list.php")
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final ArrayList<MultipleItemEntity> data =
                                new VerticalListDataConverter().setJsonData(response)
                                        .convert();
                        mRecyclerView.setAdapter(new VerticalListAdapter(data, (SortDelegate) getParentDelegate()));
                    }
                })
                .build()
                .get();
    }
}
